package org.kuan.lab.common.controller;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.kuan.lab.base.util.PageDateModel;
import org.kuan.lab.common.entity.User;
import org.kuan.lab.common.service.UserService;
import org.primefaces.context.RequestContext;
import org.primefaces.model.LazyDataModel;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component @Scope("view")
public class UserQueryComponent {
	@Resource private UserService userService;
	
	private Map<String, Object> queryMap;
	private LazyDataModel<User> lazyData;
	private User[] datas;
	
	private String beanName;
	private String methodCall;
	private String updateArea;
	
	public UserQueryComponent() {}
	
	@PostConstruct public void init() {
		queryMap = new HashMap<String, Object>(4);
		updateArea = ":option_info";
	}
	
	public void initComponent(String beanName, String methodCall, String updateArea) {
		this.beanName = beanName;
		this.methodCall = methodCall;
		this.updateArea = updateArea;
		lazyData = null;
		datas = null;
		queryMap.clear();
	}
	
	public void search() {
		List<User> list = userService.queryUsers(queryMap);
		lazyData = new PageDateModel<User>(list);
	}
	
	/**
	 * 回调方法
	 */
	public void callBack() {
		if(datas == null || datas.length == 0) {
			FacesContext.getCurrentInstance().addMessage(null, 
					new FacesMessage(FacesMessage.SEVERITY_WARN, "未选择任何用户：", "请选择要添加的用户"));
			return;
		}
		FacesContext facesContext = FacesContext.getCurrentInstance();
		Object bean = facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, beanName);
		try {
//			Method method = bean.getClass().getDeclaredMethod(methodCall, new Class[]{Object.class});
			for(Method method : bean.getClass().getMethods()) { 
				if(method.getName().equals(methodCall)) {
					method.invoke(bean, new Object[]{datas});
					break;
				}
			}
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestContext.getCurrentInstance().addCallbackParam("option_result", "success");
//		FacesContext facesContext = FacesContext.getCurrentInstance();
//		NeededBean neededBean = (NeededBean)facesContext.getApplication()
//		      .createValueBinding("#{" + beanName + "}").getValue(facesContext);
	}

	public Map<String, Object> getQueryMap() {
		return queryMap;
	}

	public void setQueryMap(Map<String, Object> queryMap) {
		this.queryMap = queryMap;
	}

	public LazyDataModel<User> getLazyData() {
		return lazyData;
	}

	public void setLazyData(LazyDataModel<User> lazyData) {
		this.lazyData = lazyData;
	}

	public User[] getDatas() {
		return datas;
	}

	public void setDatas(User[] datas) {
		this.datas = datas;
	}

	public String getBeanName() {
		return beanName;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}

	public String getMethodCall() {
		return methodCall;
	}

	public void setMethodCall(String methodCall) {
		this.methodCall = methodCall;
	}

	public String getUpdateArea() {
		return updateArea;
	}

	public void setUpdateArea(String updateArea) {
		this.updateArea = updateArea;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
